package sweep;

import javax.swing.*;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.Serial;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import java.io.*;

public class Join extends JFrame {

    private static DataOutputStream out;

    public Join(Socket client, String rival, String name) {
        JLabel RivalLabel = new JLabel(rival + " invite you to play");
        JButton AcceptButton = new JButton("accept");
        JButton RejectButton = new JButton("reject");
        AcceptButton.addActionListener(e -> {
            try {
                out = new DataOutputStream(client.getOutputStream());
                out.writeUTF("invite_yes " + rival + " " + name);
                // System.out.println("invite_yes " + rival + " " + name);
            } catch (IOException ee) {
                ee.printStackTrace();
                return;
            }
            Sweep.startWaitBoard(client, rival);
            dispose();
        });
        RejectButton.addActionListener(e -> {
            try {
                out = new DataOutputStream(client.getOutputStream());
                out.writeUTF("invite_no " + rival);
            } catch (IOException ee) {
                ee.printStackTrace();
                return;
            }
            Sweep.startMenuBoard(client);
            dispose();
        });
        RivalLabel.setBounds(50, 100 , 200 , 25);
        AcceptButton.setBounds(50, 130 , 200 , 25);
        RejectButton.setBounds(50, 160, 200, 25);
        add(RivalLabel);
        add(AcceptButton);
        add(RejectButton);
        add(new JLabel(""));
    }
}
